package com.example.p413;

import org.springframework.core.task.TaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;

@RestController
public class HelloWorldControllerCompletableFuture {
    private final TaskExecutor taskExecutor;

    public HelloWorldControllerCompletableFuture(TaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
    }

    @GetMapping("/cfuture")
    public CompletableFuture<String> hello() {
        return CompletableFuture.supplyAsync(() -> {
            randomDelay();
            return "Hello World, form Spring Boot 2!";
        }, taskExecutor);

    }

    private void randomDelay() {
        try {
            Thread.sleep(ThreadLocalRandom.current().nextInt(5000));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
